package org.intellij.doctree.node;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class FolderNodeTransferable extends DocTreeNodeTransferable {

    public static DataFlavor folderNodeFlavor;
    private static final String mimeType = DataFlavor.javaJVMLocalObjectMimeType +
            ";class=" + FolderNode.class.getName();

    static {
        try {
            folderNodeFlavor = new DataFlavor(mimeType, "DocTree Folder", FolderNode.class.getClassLoader());
        } catch (Exception e) {
            //ignore
        }
    }

    private FolderNode folderNode;

    public FolderNodeTransferable(FolderNode folderNode) {
        this.folderNode = folderNode;
    }

    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{
                folderNodeFlavor, DataFlavor.stringFlavor
        };
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return (flavor.equals(folderNodeFlavor) || flavor.isFlavorTextType());
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (flavor.equals(folderNodeFlavor)) {
            return folderNode;
        } else if (flavor.isFlavorTextType()) {
            return folderNode.getName();
        } else {
            throw new UnsupportedFlavorException(flavor);
        }
    }

    public DocTreeNode getNode() {
        return folderNode;
    }

}
